import { createApp } from 'vue'
import App from './App.vue'
import router from './router'
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'
import zhCn from 'element-plus/es/locale/lang/zh-cn'
import * as ElementPlusIconsVue from '@element-plus/icons-vue'
import { createPinia } from 'pinia'
import piniaPluginPersistedstate from 'pinia-plugin-persistedstate'
import axios from 'axios'
import { ElMessage } from 'element-plus'

const pinia = createPinia()
pinia.use(piniaPluginPersistedstate)
const app = createApp(App)
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
  app.component(key, component)
}

// 添加请求拦截器
axios.interceptors.request.use(
  config => {
    // 为所有请求添加 silent 标记
    config.silent = true;
    return config;
  },
  error => {
    return Promise.reject(error);
  }
);

// 修改响应拦截器
axios.interceptors.response.use(
  response => response,
  error => {
    // 完全静默处理错误
    return Promise.resolve({
      data: {
        code: error.response?.status || 500,
        data: null
      }
    });
  }
);

// 修改 Element Plus 的全局配置
app.config.globalProperties.$ELEMENT = {
  size: 'default',
  zIndex: 3000
};

// 覆盖全局的 ElMessage
app.config.globalProperties.$message = {
  success: (message) => {
    // 只显示关键成功提示，如登录成功
    if (message && typeof message === 'string' && message.includes('成功')) {
      ElMessage.success(message);
    }
  },
  warning: () => {},
  error: ElMessage.error,
  info: () => {}
};

// 配置 axios 默认值
axios.defaults.validateStatus = function (status) {
  return true; // 永远不会抛出 HTTP 错误
};

// 应用所有插件并挂载
app.use(pinia)
   .use(ElementPlus, {
     locale: zhCn,
     // Element Plus 消息配置
     message: {
       max: 1, // 最大显示数量，只显示一条消息
       duration: 2000 // 消息显示时长，单位毫秒
     }
   })
   .use(router)
   .mount('#app')
